/*
 * @author Robert Hanwell
 * @version 3
 * @since January
 */
package com.smartbox.floodaction;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleCursorTreeAdapter;
import android.widget.TextView;

// TODO: Auto-generated Javadoc
/**
 * The Class GroupActionPlan.
 */
public class GroupActionPlan extends ExpandableListActivity {

	/** The db helper. */
	private GroupActionData dbHelper; // your db adapter

	/** The m groups cursor. */
	private Cursor mGroupsCursor; // cursor for list of groups (list top nodes)

	/** The m group id column index. */
	private int mGroupIdColumnIndex;

	/** The m adapter. */
	private ExpandableListAdapter mAdapter;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.group_action);
		dbHelper = new GroupActionData(this);
		dbHelper.open();
		fillData();
	}

	/**
	 * Fill data.
	 */
	private void fillData() {
		mGroupsCursor = dbHelper.getGroupByAction(); // fills cursor with list
														// of
														// your top nodes -
														// groups
		startManagingCursor(mGroupsCursor);

		// Cache the ID column index
		mGroupIdColumnIndex = mGroupsCursor
				.getColumnIndexOrThrow(SQLHelper.KEY_ACT_ROWID);

		// Set up our adapter
		mAdapter = new MyExpandableListAdapter(mGroupsCursor, this,

		android.R.layout.simple_expandable_list_item_1,
				R.layout.child_taskview,

				new String[] { SQLHelper.KEY_ACT_PLAN_NAME }, // group title for
																// group layouts
				new int[] { android.R.id.text1 },

				new String[] { SQLHelper.KEY_TSK_NAME }, // exercise title for
															// child layouts
				new int[] { R.id.tvChildtask });

		setListAdapter(mAdapter);
	}

	// extending SimpleCursorTreeAdapter
	/**
	 * The Class MyExpandableListAdapter.
	 */
	public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {

		/**
		 * Instantiates a new my expandable list adapter.
		 * 
		 * @param cursor
		 *            the cursor
		 * @param context
		 *            the context
		 * @param groupLayout
		 *            the group layout
		 * @param childLayout
		 *            the child layout
		 * @param groupFrom
		 *            the group from
		 * @param groupTo
		 *            the group to
		 * @param childrenFrom
		 *            the children from
		 * @param childrenTo
		 *            the children to
		 */
		public MyExpandableListAdapter(Cursor cursor, Context context,
				int groupLayout, int childLayout, String[] groupFrom,
				int[] groupTo, String[] childrenFrom, int[] childrenTo) {
			super(context, cursor, groupLayout, groupFrom, groupTo,
					childLayout, childrenFrom, childrenTo);
		}

		// returns cursor with subitems for given group cursor
		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * android.widget.CursorTreeAdapter#getChildrenCursor(android.database
		 * .Cursor)
		 */
		@Override
		protected Cursor getChildrenCursor(Cursor groupCursor) {
			Cursor exercisesCursor = dbHelper.getTaskByAction(groupCursor
					.getLong(mGroupIdColumnIndex));
			startManagingCursor(exercisesCursor);
			return exercisesCursor;
		}

		// I needed to process click on click of the button on child item
		/*
		 * (non-Javadoc)
		 * 
		 * @see android.widget.CursorTreeAdapter#getChildView(int, int, boolean,
		 * android.view.View, android.view.ViewGroup)
		 */
		@Override
		public View getChildView(final int groupPosition,
				final int childPosition, boolean isLastChild, View convertView,
				ViewGroup parent) {
			View rowView = super.getChildView(groupPosition, childPosition,
					isLastChild, convertView, parent);

			TextView details = (TextView) rowView
					.findViewById(R.id.tvChildtask);

			details.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					Cursor exerciseCursor = getChild(groupPosition,
							childPosition);

					Long exerciseId = exerciseCursor.getLong(exerciseCursor
							.getColumnIndex(SQLHelper.KEY_ACT_ROWID));

					Intent i = new Intent(GroupActionPlan.this,
							SingleTaskView.class);
					i.putExtra(SQLHelper.KEY_ACT_ROWID, exerciseId);
					startActivity(i);
				}
			});

			return rowView;
		}

	}

	// after create start database connection
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		super.onStart();
		Log.i("onStart", "open database");
		dbHelper.open();

	}

	// before we close the screen close the database
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		super.onStop();
		Log.i("onStop", "closing database");
		dbHelper.close();

	}

}
